// pages/dizhiguanli/dizhiguanli.js
const request = require("../../utils/request.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cur: {},
    dzList: "",
    dizhi: "",
    id: "",
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 地址列表
    this.getdizhiList();
  },
  // 获取地址列表
  getdizhiList() {
    const _this = this;
    request.wxRequest(
      "address/list",
      function (err, data) {
        // console.log(data.data.list)
        if (err) {
          // 失败；
        } else {
          _this.setData({
            dizhi: data.data.list,
          });
        }
      },
      "GET",
      {
        user_id: 1,
      }
    );
  },

  del (a) {
    const id = a.currentTarget.dataset["index"];
    // console.log(id)
    request.wxRequest(
      "address/del",
      function (err, data) {
        if (err) {
          // 失败；
        } else {
        }
      },
      "POST",
      {
        id: id,
      }
    );
    this.onLoad();
  },
  look (e) {
    const { item } = e.currentTarget.dataset;
    this.setData(
      {
        cur: item,
      },
      () => {
        this.setData({ visible: true });
      }
    );
    const id = e.currentTarget.dataset["index"];
    this.setData({
      id: id,
    });
    // console.log(id)
    const _this = this;
    request.wxRequest(
      "address/edit1",
      function (err, data) {
        if (err) {
          // 失败；
        } else {
          // console.log(data.data.list);
          _this.setData({
            dzList: data.data.list,
          });
        }
      },
      "POST",
      {
        id: id,
      }
    );
  },
  back_houtai: function (e) {
    const id = this.data.id;
    console.log("id:" + id);
    //姓名
    var total_score1 = e.detail.value.total_score1;
    //手机号
    var total_score2 = e.detail.value.total_score2;
    //地址
    var total_score3 = e.detail.value.total_score3;
    //详细地址
    var total_score4 = e.detail.value.total_score4;
    // console.log('姓名：'+total_score1)
    // console.log('手机号:'+total_score2)
    // console.log('地址:'+total_score3)
    // console.log('详细地址:'+total_score4)

    // 新增地址
    this.adddizhiList(
      id,
      total_score1,
      total_score2,
      total_score3,
      total_score4
    );
    wx.navigateTo({
      url: "/pages/dizhiguanli/dizhiguanli",
    });
  },
  // 新增地址
  adddizhiList(
    id,
    total_score1,
    total_score2,
    total_score3,
    total_score4
  ) {
    // console.log('id:'+id,total_score1,total_score2,total_score3,total_score4)
    request.wxRequest(
      "address/edit2",
      function (err, data) {
        // console.log(data)
        if (err) {
          // 失败；
        } else {
          this.getdizhiList()
        }
      },
      "POST",
      {
        id: id,
        name: total_score1,
        mobile: total_score2,
        area: total_score3,
        address: total_score4,
      }
    );
  },
  jump () {
    wx.navigateTo({ url: "/pages/addmogo/addmo" });
  },

  onVisibleChange (e) {
    this.setData({
      visible: e.detail.visible,
    });
  },
  onClose () {
    this.setData({
      visible: false,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
